<?php

namespace app\admin\model;

use think\Model;

/**
 * 企业社群
 * Class Community
 * @package app\admin\model
 */
class Community extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'enterprise_resource';

    /**
     * 企业社群列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @return array
     */
    public static function getCommunityList($where, $page, $pagesize)
    {
        $data = self::alias('r')
            ->join('enterprise e', 'e.id = r.eid', 'left')
            ->join('users u', 'u.id = r.uid', 'left')
            ->join('industry_cate c', 'c.id = r.cid', 'left')
            ->field('r.*,e.name as ename,e.city as ecity,u.headimg,u.nickname,c.name as cate_name')
            ->where($where)
            ->page($page, $pagesize)
            ->order('r.create_time', 'desc')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 企业社群统计
     * @param $where
     * @return int
     */
    public static function countCommunityList($where)
    {
        return self::alias('r')
            ->join('enterprise e', 'e.id = r.eid', 'left')
            ->join('users u', 'u.id = r.uid', 'left')
            ->join('resource_cate c', 'c.id = r.cid', 'left')
            ->where($where)
            ->count();
    }

    /**
     * 指定企业社群
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findCommunity($where)
    {
        $info = self::where($where)->find();

        return $info ? $info->toArray() : [];
    }

    /**
     * 更新企业社群
     * @param $data
     * @param $where
     * @return Community
     */
    public static function updateCommunity($data, $where)
    {
        return self::update($data, $where);
    }

    /**
     * 创建企业社群
     * @param $data
     * @return Community|Model
     */
    public static function createCommunity($data)
    {
        return self::create($data);
    }
}